﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;

namespace Qplus.UI
{
    public partial class RecordControl : UserControl
    {
        msgTypeEnum msgType;
        public RecordControl()
        {
            InitializeComponent();
            recCtrlBtn.AddHandler(ButtonEx.MouseLeftButtonDownEvent, new MouseButtonEventHandler(CtrlBtn_MouseLeftButtonDown), true);
            recCtrlBtn.AddHandler(ButtonEx.MouseLeftButtonUpEvent, new MouseButtonEventHandler(CtrlBtn_MouseLeftButtonUp), true);
            //PhoneTextBox pTb = new PhoneTextBox();
        }

        public msgTypeEnum MsgType
        {
            set
            {
                switch (value)
                {
                    case msgTypeEnum.receiveTxt:
                        {
                            msgType = value;
                            otherHead.Visibility = System.Windows.Visibility.Visible;
                            receiveGrid.Visibility = System.Windows.Visibility.Visible;
                            break;
                        }
                    case msgTypeEnum.receiveVoice:
                        {
                            msgType = value;
                            otherHead.Visibility = System.Windows.Visibility.Visible;
                            receiveGrid.Visibility = System.Windows.Visibility.Visible;
                            receiveVoice.Visibility = System.Windows.Visibility.Visible;
                            break;
                        }
                    case msgTypeEnum.receivePic:
                        {
                            msgType = value;
                            otherHead.Visibility = System.Windows.Visibility.Visible;
                            picImg.Visibility = System.Windows.Visibility.Visible;
                            picImg.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            break;
                        }
                    case msgTypeEnum.sendTxt:
                        {
                            msgType = value;
                            myHead.Visibility = System.Windows.Visibility.Visible;
                            sendGrid.Visibility = System.Windows.Visibility.Visible;
                            break;
                        }
                    case msgTypeEnum.sendVoice:
                        {
                            msgType = value;
                            myHead.Visibility = System.Windows.Visibility.Visible;
                            sendGrid.Visibility = System.Windows.Visibility.Visible;
                            sendVoice.Visibility = System.Windows.Visibility.Visible;
                            break;
                        }
                    case msgTypeEnum.sendPic:
                        {
                            msgType = value;
                            myHead.Visibility = System.Windows.Visibility.Visible;
                            picImg.Visibility = System.Windows.Visibility.Visible;
                            picImg.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                            break;
                        }
                    case msgTypeEnum.notype:
                    default:
                        {
                            break;
                        }
                }
            }
            get { return msgType; }
        }
        public string Msg
        {
            set
            {
                switch (msgType)
                {
                    case msgTypeEnum.receiveTxt:
                        {
                            recTxt.Text = value;
                            break;
                        }
                    case msgTypeEnum.receiveVoice:
                        {
                            receiveVoiceLengthTxt.Text = value;
                            break;
                        }
                    case msgTypeEnum.sendTxt:
                        {
                            senTxt.Text = value;
                            break;
                        }
                    case msgTypeEnum.sendVoice:
                        {
                            sendVoiceLengthTxt.Text = value;
                            break;
                        }
                    case msgTypeEnum.notype:
                    default:
                        {
                            break;
                        }
                }
            }
        }
        public int VoiceLength
        {
            set
            {
                switch (this.MsgType)
                {
                    case msgTypeEnum.receiveVoice:
                        {
                            receiveVoice.Width = value > 275 ? 275 : value;
                            break;
                        }
                    case msgTypeEnum.sendVoice:
                        {
                            sendVoice.Width = value > 275 ? 275 : value;
                            break;
                        }
                    case msgTypeEnum.notype:
                    default:
                        {
                            break;
                        }
                }
            }
        }
        public BitmapImage Pic
        {
            set { picImg.Source = value; }
            get { return (BitmapImage)picImg.Source; }
        }
        private void CtrlBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            switch(this.MsgType)
            {
                case msgTypeEnum.receiveTxt:
                case msgTypeEnum.receiveVoice:
                    {
                        receiveRecPress.Visibility = System.Windows.Visibility.Visible;
                        receivepressImg.Visibility = System.Windows.Visibility.Visible;
                        break;
                    }
                case msgTypeEnum.sendTxt:
                case msgTypeEnum.sendVoice:
                    {
                        sendRecPress.Visibility = System.Windows.Visibility.Visible;
                        sendpressImg.Visibility = System.Windows.Visibility.Visible;
                        break;
                    }
                case msgTypeEnum.notype:
                default:
                    {
                        break;
                    }
            }
        }
        private void CtrlBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            switch (this.MsgType)
            {
                case msgTypeEnum.receiveTxt:
                    {
                        receiveRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        receivepressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.receiveVoice:
                    {
                        receiveRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        receivepressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.sendTxt:
                    {
                        sendRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        sendpressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.sendVoice:
                    {
                        sendRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        sendpressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.notype:
                default:
                    {
                        break;
                    }
            }
        }
        private void CtrlBtn_LostMouseCapture(object sender, MouseEventArgs e)
        {
            switch (this.MsgType)
            {
                case msgTypeEnum.receiveTxt:
                case msgTypeEnum.receiveVoice:
                    {
                        receiveRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        receivepressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.sendTxt:
                case msgTypeEnum.sendVoice:
                    {
                        sendRecPress.Visibility = System.Windows.Visibility.Collapsed;
                        sendpressImg.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    }
                case msgTypeEnum.notype:
                default:
                    {
                        break;
                    }
            }
        }
        SmileyControl smiley;
        public SmileyControl Smiley
        {
            set { smiley = new SmileyControl() { Index = value.Index }; Grid.SetColumn(smiley, 1); receiveGrid.Children.Add(smiley); }
        }

        #region TEST
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // NOTE: the paragraphs with the actual content are
            // wrapped in a section that defines the namespace so that the elements can be resolved
            string xaml =
                @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                    <Paragraph>
                        <Underline Foreground=""LightBlue"">Some Underline Text Here!</Underline>
                    </Paragraph>
                    <Paragraph Foreground=""Red"">
                        <Bold>Some Bold Text Here!</Bold>
                    </Paragraph>
                </Section>";
            this.richTextBox.Xaml = xaml;
        }
        #endregion
    }
}
